package top.laoshuzi.android.iotcamera.camera.ui

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import kotlinx.android.synthetic.main.fragment_camera.*
import org.koin.android.viewmodel.ext.android.viewModel
import top.defaults.colorpicker.ColorPickerPopup
import top.laoshuzi.android.iotcamera.R
import top.laoshuzi.android.iotcamera.camera.bean.dto.LightStateCmdDTO
import top.laoshuzi.android.iotcamera.camera.viewmodel.CameraViewModel
import top.laoshuzi.android.iotcamera.camera.bean.dto.CameraTargetDTO


class CameraFragment : Fragment() {

    private val cameraViewModel: CameraViewModel by viewModel()

    private val id = "11771230"

    @SuppressLint("SetTextI18n")
    override fun onAttach(context: Context?) {
        super.onAttach(context)

        cameraViewModel.subscribeLightState(id)

        cameraViewModel.lightStateObserver
            .observe(this, Observer<LightStateCmdDTO> { state ->
                view_green_led.text = "绿:${state.led_g}"
                view_red_led.text = "红:${state.led_r}"
                view_blue_led.text = "蓝:${state.led_b}"
                view_light_color.setBackgroundColor(
                    Color.argb(
                        0xFF,
                        state.led_r?.let { it } ?: 0,
                        state.led_g?.let { it } ?: 0,
                        state.led_b?.let { it } ?: 0
                    )
                )
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_camera, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        //灯控制
        btn_color_picker.setOnClickListener {
            ColorPickerPopup.Builder(activity)
                .initialColor(0xFFFFFFFF.toInt())
                .enableBrightness(true)
                .enableAlpha(false)
                .okTitle("确定")
                .cancelTitle("取消")
                .showIndicator(true)
                .showValue(true)
                .build()
                .show(object : ColorPickerPopup.ColorPickerObserver() {
                    override fun onColorPicked(color: Int) {
                        cameraViewModel.publishLightState(
                            id,
                            LightStateCmdDTO().apply {
                                led_g = Color.green(color)
                                led_r = Color.red(color)
                                led_b = Color.blue(color)
                            })
                    }
                })
        }

        //相机控制
//        view_horizontal_rocker.setListener { eventType, currentAngle, currentDistance ->
//            if (RockerView.EVENT_ACTION == eventType) {
//                println("$eventType--${view_horizontal_rocker.areaRadius}--$currentAngle--$currentDistance")
//            }
//        }
        view_horizontal_seek.setOnSeekBarChangeListener(
            object : SeekBar.OnSeekBarChangeListener {
                override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                    cameraViewModel.publishCameraControlDirection(
                        CameraTargetDTO().apply {
                            deviceId = id
                            num = 1
                            angular = seekBar.progress
                        }
                    )
                }

                override fun onStartTrackingTouch(seekBar: SeekBar) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar) {
                }
            })
        view_vertical_seek.setOnSeekBarChangeListener(
            object : SeekBar.OnSeekBarChangeListener {
                override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                    cameraViewModel.publishCameraControlDirection(
                        CameraTargetDTO().apply {
                            deviceId = id
                            num = 0
                            angular = seekBar.progress
                        }
                    )
                }

                override fun onStartTrackingTouch(seekBar: SeekBar) {
                }

                override fun onStopTrackingTouch(seekBar: SeekBar) {
                }
            })


    }


}